#include "bga/graphics/CameraAnimation.h"
#include <iostream>
#include <SFML/System/Clock.hpp>

#define EPSILON 10e-3

//static sf::Clock cfinal;

namespace bga
{

CameraAnimation::CameraAnimation(float animationTime, Camera* camera)
                                            : animationTime(animationTime),
                                              camera(camera),
                                              currentT(0.f)
{
    /// We are going to do interpolate the quaternion 60 times per second
    this->timer.setTime(100.f/6.f);
    this->timer.addTimerListener(*this);
}

CameraAnimation::~CameraAnimation()
{

}

void CameraAnimation::startAnimation( const Quaternion& initial, const Quaternion& final)
{
    if(!this->timer.isStarted() && initial != final)
    {
        this->initial = initial;
        this->final = final;
        currentT = 100.f/(6.f * this->animationTime);
        this->timer.start();
        //cfinal.Reset();

    }
}

bool CameraAnimation::isRunning() const
{
    return this->timer.isStarted();
}

void CameraAnimation::setCamera(Camera* camera)
{
    this->camera = camera;
}

void CameraAnimation::timeElapsed(const bga::TimerEvent &event)
{
    Quaternion res = this->initial.slerp(this->final, currentT);
    this->camera->setDirection(res);
    if(currentT >= 1 - EPSILON)
    {
        this->timer.stop();
        //std::cout << "Timer last " << cfinal.GetElapsedTime() << std::endl;
    }
    currentT += 100.f/(6.f * this->animationTime);
}


}
